package org.redis.cluster.command;

import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.collections.CollectionUtils;
import org.redis.cluster.config.ClientConfig;
import org.redis.cluster.processor.CommandProcessor;
import org.redis.cluster.redis.RedisCommand;
import org.redis.cluster.redis.transport.RedisReply;
import org.redis.cluster.redis.transport.reply.RedisBulkReply;
import org.redis.cluster.redis.transport.reply.RedisErrorReply;
import org.redis.cluster.redis.transport.reply.RedisMultipleBulkReply;
import org.redis.cluster.transport.client.support.ClientPoolMonitor;
import org.redis.cluster.transport.session.Session;

public class ClientsCommandProcessor implements CommandProcessor {
	
	private final static String COMMAND="CLIENTS";

	@Override
	public RedisReply process(RedisCommand command, Session session) {
		Collection<ClientConfig> clients=getClients();
		if(CollectionUtils.isEmpty(clients)) {
			return new RedisErrorReply("no clients");
		}
		
		Collection<RedisReply> replys=new ArrayList<RedisReply>(clients.size());
		for(ClientConfig client:clients) {
			replys.add(new RedisBulkReply(client));
		}
		
		return new RedisMultipleBulkReply(replys);
	}

	@Override
	public String getSupportCommand() {
		return COMMAND;
	}
	
	protected Collection<ClientConfig> getClients() {
		return ClientPoolMonitor.getConfigs();
	}

}